package com.connectlinkcorp.common.ui.demo;

import java.util.ArrayList;

import com.connectlinkcorp.common.ui.horizontalscrollnumbertiles.HorizontalScrollNumberTiles;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class DragDropActivity extends Activity implements OnTouchListener{

	int imageDigit[] = new int[]{
			R.drawable.digit00,R.drawable.digit01,R.drawable.digit02,R.drawable.digit03,R.drawable.digit04,
			R.drawable.digit05,R.drawable.digit06,R.drawable.digit07,R.drawable.digit08,R.drawable.digit09,
			R.drawable.digit10,R.drawable.digit11,R.drawable.digit12,R.drawable.digit13,R.drawable.digit14,
			R.drawable.digit15,R.drawable.digit16,R.drawable.digit17,R.drawable.digit18,R.drawable.digit19,
			R.drawable.digit20,R.drawable.digit21,R.drawable.digit22,R.drawable.digit23,R.drawable.digit24,
			R.drawable.digit25,R.drawable.digit26,R.drawable.digit27,R.drawable.digit28,R.drawable.digit29,
			R.drawable.digit30,R.drawable.digit31,R.drawable.digit32,R.drawable.digit33,R.drawable.digit34,
			R.drawable.digit35,R.drawable.digit36,R.drawable.digit37,R.drawable.digit38,R.drawable.digit39,
			R.drawable.digit40,R.drawable.digit41,R.drawable.digit42,R.drawable.digit43,R.drawable.digit44,
			R.drawable.digit45,R.drawable.digit46,R.drawable.digit47,R.drawable.digit48,R.drawable.digit49,
			R.drawable.digit50,R.drawable.digit51,R.drawable.digit52,R.drawable.digit53,R.drawable.digit54,
			R.drawable.digit55,R.drawable.digit56,R.drawable.digit57,R.drawable.digit58,R.drawable.digit59,
			R.drawable.digit60,R.drawable.digit61,R.drawable.digit62,R.drawable.digit63,R.drawable.digit64,
			R.drawable.digit65,R.drawable.digit66,R.drawable.digit67,R.drawable.digit68,R.drawable.digit69,
			R.drawable.digit70,R.drawable.digit71,R.drawable.digit72,R.drawable.digit73,R.drawable.digit74,
			R.drawable.digit75,R.drawable.digit76,R.drawable.digit77,R.drawable.digit78,R.drawable.digit79,
			R.drawable.digit80,R.drawable.digit81,R.drawable.digit82,R.drawable.digit83,R.drawable.digit84,
			R.drawable.digit85,R.drawable.digit86,R.drawable.digit87,R.drawable.digit88,R.drawable.digit89,
			R.drawable.digit90,R.drawable.digit91,R.drawable.digit92,R.drawable.digit93,R.drawable.digit94,
			R.drawable.digit95,R.drawable.digit96,R.drawable.digit97,R.drawable.digit98,R.drawable.digit99};
		HorizontalScrollNumberTiles numberView;
		ArrayList<Integer> numbers;
	
		
	private Button btnNum1;
	private Button btnNum2;
	private Button btnDrapDrop;
	private Point pointDown;
	private int layout_x = 0;
	private int layout_y = 0;
	private int dx = 0;
	private int dy = 0;
	private int prwidth;
	private int prheight;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dragdrop);
		
		loadCOntrol();
	}
	
	/*
	 * 
	 */
	private void loadCOntrol(){
		btnNum1 = (Button)findViewById(R.id.btnNum1);
		btnNum2 = (Button)findViewById(R.id.btnNum2);
		btnDrapDrop = (Button)findViewById(R.id.btnDrapDrop);
		
		btnDrapDrop.setOnTouchListener(this);
		
		int [] numbersArr = new int []{1, 2, 12 ,2, 2, 12};
		numbers = new ArrayList<Integer>();
		for (int i = 0; i < numbersArr.length; i++) {
			numbers.add(numbersArr[i]);
		}
		numberView = (HorizontalScrollNumberTiles)findViewById(R.id.numberTileView);
		
		// set up param
		numberView.setUpParam(this,4,50,150,400,200,numbers);
		// set resource background for digit
		numberView.setImageDigit(imageDigit);
		// show number
		numberView.show();
		// set click event
		numberView.setItemClicked(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Toast.makeText(DragDropActivity.this,
						String.valueOf(numberView.getNumbers().get(v.getId())), Toast.LENGTH_SHORT).show();
				btnDrapDrop.bringToFront();
				prwidth = v.getWidth();
				prheight = v.getHeight();
				RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(prwidth,prheight);;
				pointDown = new Point(numberView.getPositionX(),numberView.getPositionY()-20);
				params.setMargins(pointDown.x,pointDown.y, 0,0);
				btnDrapDrop.setLayoutParams(params);
				btnDrapDrop.setBackgroundResource(imageDigit[numbers.get(v.getId())]);
				
			}
		});
		// set touch 
		/*numberView.setItemTouch(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				btnDrapDrop.bringToFront();
				RelativeLayout.LayoutParams params = null;
				pointDown = new Point(numberView.getPositionX(),numberView.getPositionY());
				dx = (int)event.getRawX() - v.getLeft();
				dy = (int)event.getRawY() - v.getTop();
				layout_x = (int)event.getRawX() - dx;
				layout_y = (int)event.getRawY() - dy;
				params = new RelativeLayout.LayoutParams(100,100);
				params.setMargins(pointDown.x,pointDown.y, 0,0);
				btnDrapDrop.setLayoutParams(params);
				return false;
			}
		});*/
	}

	private void dragDropView(View v, MotionEvent event){
		RelativeLayout.LayoutParams params = null;
		switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				pointDown = new Point(btnDrapDrop.getLeft(),btnDrapDrop.getTop());
				dx = (int)event.getRawX() - btnDrapDrop.getLeft();
				dy = (int)event.getRawY() - btnDrapDrop.getTop();
				layout_x = (int)event.getRawX() - dx;
				layout_y = (int)event.getRawY() - dy;
				params = new RelativeLayout.LayoutParams(prwidth,prheight);
				params.setMargins(layout_x,layout_y, 0,0);
				btnDrapDrop.setLayoutParams(params);
				break;
				
			case MotionEvent.ACTION_MOVE:
				btnDrapDrop.bringToFront();
				layout_x = (int)event.getRawX() - dx;
				layout_y = (int)event.getRawY() - dy;
				params = new RelativeLayout.LayoutParams(prwidth,prheight);
				params.setMargins(layout_x,layout_y, 0,0);
				btnDrapDrop.setLayoutParams(params);
				break;
				
			case MotionEvent.ACTION_UP:
				
				break;
		}
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		dragDropView(v,event);
		return true;
	}

	
}






















